<?php

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

// #################################################################### ||
// #                                                                  # ||
// #   vBulletin 3.x.x to MySmartBB 1.1.2 script                      # ||
// #   http://www.MySmartBB.com                                       # ||
// #   Copyright (c) 2005 by MySmartBB team 			              # ||
// #                                                                  # ||
// #   filename :  class.php                                          # ||
// #   the main class , all functions in this class                   # ||
// #                                                                  # ||
// #################################################################### ||

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

include("mainclass.php");

class VB2MySBB extends MainClass {

var $_BITFIELD = array(
    	'canview'           => 1,
        'canviewothers'     => 2,
        'cansearch'         => 4,
        'canemail'          => 8,
        'canpostnew'        => 16,
        'canreplyown'       => 32,
        'canreplyothers'    => 64,
        'caneditpost'       => 128,
        'candeletepost'     => 256,
        'candeletethread'   => 512,
        'canopenclose'      => 1024,
        'canmove'           => 2048,
        'cangetattachment'  => 4096,
        'canpostattachment' => 8192,
        'canpostpoll'       => 16384,
        'canvote'           => 32768,
        'canthreadrate'     => 65536,
        'isalwaysmoderated' => 131072,
        'canseedelnotice'   => 262144
);

var $usergroup_id = array(
        	#VB | Smart
            1        =>     7,
            2  		 =>     4,
            5        =>		2,
            6        =>     1,
            7        =>     3,
            8        =>     6
);

/************************************************/

## these functions From vBulletin

function vbdate($format, $timestamp = TIMENOW, $doyestoday = false, $locale = true, $adjust = true, $gmdate = false)
{
//        global $bbuserinfo, $vboptions, $vbphrase;

        $hourdiff = $vboptions['hourdiff'];

        if ($bbuserinfo['lang_locale'] AND $locale)
        {
                if ($gmdate)
                {
                        $datefunc = 'gmstrftime';
                }
                else
                {
                        $datefunc = 'strftime';
                }
        }
        else
        {
                if ($gmdate)
                {
                        $datefunc = 'gmdate';
                }
                else
                {
                        $datefunc = 'date';
                }
        }
        if (!$adjust)
        {
                $hourdiff = 0;
        }
        $timestamp_adjusted = max(0, $timestamp - $hourdiff);

        if ($format == $vboptions['dateformat'] AND $doyestoday AND $vboptions['yestoday'])
        {
                if ($vboptions['yestoday'] == 1)
                {
                        if (!defined('TODAYDATE'))
                        {
                                define ('TODAYDATE', vbdate('n-j-Y', TIMENOW, false, false));
                                define ('YESTDATE', vbdate('n-j-Y', TIMENOW - 86400, false, false));
                                define ('TOMDATE', vbdate('n-j-Y', TIMENOW + 86400, false, false));
                           }

                        $datetest = @date('n-j-Y', $timestamp - $hourdiff);

                        if ($datetest == TODAYDATE)
                        {
                                $returndate = $vbphrase['today'];
                        }
                        else if ($datetest == YESTDATE)
                        {
                                $returndate = $vbphrase['yesterday'];
                        }
                        else
                        {
                                $returndate = $datefunc($format, $timestamp_adjusted);
                        }
                }
                else
                {
                        $timediff = TIMENOW - $timestamp;

                        if ($timediff < 3600)
                        {
                                if ($timediff < 120)
                                {
                                        $returndate = $vbphrase['1_minute_ago'];
                                }
                                else
                                {
                                        $returndate = construct_phrase($vbphrase['x_minutes_ago'], intval($timediff / 60));
                                }
                        }
                        else if ($timediff < 7200)
                        {
                                $returndate = $vbphrase['1_hour_ago'];
                        }
                        else if ($timediff < 86400)
                        {
                                $returndate = construct_phrase($vbphrase['x_hours_ago'], intval($timediff / 3600));
                        }
                        else if ($timediff < 172800)
                        {
                                $returndate = $vbphrase['1_day_ago'];
                        }
                        else if ($timediff < 604800)
                        {
                                $returndate = construct_phrase($vbphrase['x_days_ago'], intval($timediff / 86400));
                        }
                        else if ($timediff < 1209600)
                        {
                                $returndate = $vbphrase['1_week_ago'];
                        }
                        else if ($timediff < 3024000)
                        {
                                $returndate = construct_phrase($vbphrase['x_weeks_ago'], intval($timediff / 604900));
                        }
                        else
                        {
                                $returndate = $datefunc($format, $timestamp_adjusted);
                        }
                }

                return $returndate;
        }
        else
        {
                return $datefunc($format, $timestamp_adjusted);
        }
}

function convert_bits_to_array(&$bitfield, $_FIELDNAMES)
{
        $bitfield = intval($bitfield);
        $arry = array();
        foreach ($_FIELDNAMES AS $field => $bitvalue)
        {
                if ($bitfield & $bitvalue)
                {
                        $arry["$field"] = 1;
                }
                else

                {
                        $arry["$field"] = 0;
                }
        }
        return $arry;
}

## End vBulletin functions

/************************************************/

function change_string($info)
{
    $search = @strstr($info,$info);

        if ($search)
        {
                $info = str_replace('&#1583;','د',$info);
                $info = str_replace('&#1580;','ج',$info);
                $info = str_replace('&#1581;','ح',$info);
                $info = str_replace('&#1582;','خ',$info);
                $info = str_replace('&#1607;','ه',$info);
                $info = str_replace('&#1593;','ع',$info);
                $info = str_replace('&#1594;','غ',$info);
                $info = str_replace('&#1601;','ف',$info);
                $info = str_replace('&#1602;','ق',$info);
                $info = str_replace('&#1579;','ث',$info);
                $info = str_replace('&#1589;','ص',$info);
                $info = str_replace('&#1590;','ض',$info);
                $info = str_replace('&#1591;','ط',$info);
                $info = str_replace('&#1603;','ك',$info);
                $info = str_replace('&#1605;','م',$info);
                $info = str_replace('&#1606;','ن',$info);
                $info = str_replace('&#1578;','ت',$info);
                $info = str_replace('&#1575;','ا',$info);
                $info = str_replace('&#1604;','ل',$info);
                $info = str_replace('&#1576;','ب',$info);
                $info = str_replace('&#1610;','ي',$info);
                $info = str_replace('&#1587;','س',$info);
                $info = str_replace('&#1588;','ش',$info);
                $info = str_replace('&#1592;','ظ',$info);
                $info = str_replace('&#1586;','ز',$info);
                $info = str_replace('&#1608;','و',$info);
                $info = str_replace('&#1577;','ة',$info);
                $info = str_replace('&#1609;','ى',$info);
                $info = str_replace('&#1604;','لا',$info);
                $info = str_replace('&#1585;','ر',$info);
                $info = str_replace('&#1575;','ؤ',$info);
                $info = str_replace('&#1572;','ء',$info);
                $info = str_replace('&#1569;','ئ',$info);
                $info = str_replace('&#1614;','َ',$info);
                $info = str_replace('&#1611;','ً',$info);
                $info = str_replace('&#1615;','ُ',$info);
                $info = str_replace('&#1612;','ٌ',$info);
                $info = str_replace('&#1604;&#1573;','لإ',$info);
                $info = str_replace('&#1573;','إ',$info);
                $info = str_replace('&#1616;','ِ',$info);
                $info = str_replace('&#1613;','ٍ',$info);
                $info = str_replace('&#1604;&#1571;','لأ',$info);
                $info = str_replace('&#1571;','أ',$info);
                $info = str_replace('&#1600;','ـ',$info);
                $info = str_replace('&#1618;','ْ',$info);
                $info = str_replace('&#1604;&#1570;','لآ',$info);
                $info = str_replace('&#1570;','آ',$info);
        }

        $info = str_replace('<br />','
',$info);
        $info = str_replace('[center]','',$info);
        $info = str_replace('[/center]','',$info);

        return $info;
}

/************************************************/

function random()
{
                $date_ = str_replace("/","",$date);
        $s = time() . $date_ . rand(1,10000) . microtime();
        $s = str_replace(",","",$s);
        $s = str_replace(" ","",$s);

        $s2 = time()*85200 . $date_*1000 . rand(1,10000) . microtime();
        $s2 = str_replace(",","",$s2);
        $s2 = str_replace(" ","",$s2);

        $s = md5($s).md5($s2);
        $s = substr($s,0,26);

        return $s;
}

/************************************************/

  function send_mail($to,$subject,$message,$sender)
  {
  		$hdr   = "Content-Type: text/html; charset=windows-1256\n";
        $hdr  .= "From:"."$sender\n";
        $email = mail($to,$subject,$message,$hdr);

        if ($email)
        {
        	return 1;
        }
        else
        {
        	return 0;
        }
  }

/************************************************/

# Sections
function import_main_section()
{
	global $DB,$SF;

    $getfromsection = $DB->sql_query("SELECT * FROM " . $table_begin . "forum WHERE parentid='-1'");

    while ($getfromrow = $DB->sql_fetch_array($getfromsection))
    {
    	$insertnsection = $DB->sql_query("INSERT INTO MySBB_section(id,title,section_describe,main_section,sort) VALUES
        ('" . $getfromrow['forumid'] . "','" .  $this->change_string($getfromrow['title']) . "','" .  $this->change_string($getfromrow['description']) . "','1','" . $getfromrow['displayorder'] . "')");

        $groupinfo_query = $DB->sql_query("SELECT * FROM MySBB_group ORDER BY id ASC");

        while ($groupinfo_row = $DB->sql_fetch_array($groupinfo_query))
        {
        	$insert_group = $DB->sql_query("INSERT INTO MySBB_sectiongroup
            (id,section_id,group_id,view_section,download_attach,write_subject,write_reply,upload_attach,edit_own_subject,edit_own_reply,del_own_subject,del_own_reply,write_poll,vote_poll)
            VALUES
            ('NULL','" . $getfromrow['forumid'] . "','" . $groupinfo_row['id'] . "','" . $groupinfo_row['view_section'] . "','" . $groupinfo_row['download_attach'] . "','" . $groupinfo_row['write_subject'] . "','" . $groupinfo_row['write_reply'] . "','" . $groupinfo_row['upload_attach'] . "','" . $groupinfo_row['edit_own_subject'] . "','" . $groupinfo_row['edit_own_reply'] . "','" . $groupinfo_row['del_own_subject'] . "','" . $groupinfo_row['del_own_reply'] . "','" . $groupinfo_row['write_poll'] . "','" . $groupinfo_row['vote_poll'] . "')");
        }

        if ($insertnsection)
        {
        	$SF->msg("تم نقل " . $getfromrow['forumid']);
        }
        if (!$insertnsection)
        {
        	$SF->msg("لم يتم نقل" . $getfromrow['forumid']);
        }
    }
}

/************************************************/

function import_normal_section()
{
    global $DB,$SF;

    $getfromsection = $DB->sql_query("SELECT * FROM " . $table_begin . "forum WHERE parentid<>'-1'");

    while ($getfromrow = $DB->sql_fetch_array($getfromsection))
    {
    	$linksection = 0;

        if (!empty($getfromrow['link']))
        {
                $linksection = 1;
        }

        $insertnsection = $DB->sql_query("INSERT INTO MySBB_section
        (id,title,section_describe,main_section,sort,from_main_section,section_password,linksite,show_sig,usesmartcode_allow,linksection)
        VALUES
        ('" . $getfromrow['forumid'] . "','" .  $this->change_string($getfromrow['title']) . "','" .  $this->change_string($getfromrow['description']) . "','0','" . $getfromrow['displayorder'] . "','" . $getfromrow['parentid'] . "','" . $getfromrow['password'] . "','" . $getfromrow['link'] . "','1','1','" . $linksection . "')");

        if ($insertnsection)
        {
        	$SF->msg("تم نقل " . $getfromrow['forumid']);
        }
        if (!$insertnsection)
        {
        	$SF->msg("لم يتم نقل" . $getfromrow['forumid']);
        }
    }
}

/************************************************/

function import_sub_section()
{

	global $DB,$SF;

    $getnormalsection_query = $DB->sql_query("SELECT * FROM " . $table_begin . "forum WHERE parentid<>'-1'");

    while ($getnormalsection_row = $DB->sql_fetch_array($getnormalsection_query))
    {
    	$getsubsection_query = $DB->sql_query("SELECT * FROM " . $table_begin . "forum WHERE parentid='" . $getnormalsection_row['forumid'] . "'");
        while ($getsubsection_row = $DB->sql_fetch_array($getsubsection_query))
        {
                $update = $DB->sql_query("UPDATE MySBB_section SET
                                                                sub_section='1',
                                                                from_sub_section='" . $getsubsection_row['parentid'] . "'
                                                        WHERE id='" . $getsubsection_row['forumid'] . "'
                                                      ");

                if ($update)
                {
                    $SF->msg('تم تحويل ' . $getsubsection_row['forumid']);
                }
                else
                {
                    $SF->msg('لم يتم تحويل ' . $getsubsection_row['forumid']);
                }
        }
    }

}

/************************************************/

 # Members
function import_member()
{

	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "user");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getfrommember = $DB->sql_query("SELECT * FROM " . $table_begin . "user LIMIT $count,$perpage");

    while ($getfromrow = $DB->sql_fetch_array($getfrommember))
    {
                   $insertnmember = $DB->sql_query("INSERT INTO
                   MySBB_member(id,username,password,email,user_gender,register_date,posts,user_title,style)
                   VALUES
                   ('" . $getfromrow['userid'] . "','" .  $this->change_string(addslashes($getfromrow['username'])) . "','" .  $getfromrow['password'] . "','" .  $getfromrow['email'] . "','ذكر','" . $getfromrow['passworddate'] . "','" . $getfromrow['posts'] . "','" . $getfromrow['usertitle'] . "','1')");

                   if ($insertnmember)
                   {
                   		$SF->msg("تم نقل " . $this->change_string(htmlspecialchars($getfromrow['userid'])));
                   }
                   if (!$insertnmember)
                   {
                   		$SF->msg("لم يتم نقل " . $this->change_string(htmlspecialchars($getfromrow['userid'])));
                   }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
    	echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=7'>تحويل الرسائل الخاصه</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=6&count=$next'>$i</a>";
                }

                $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=6&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

 # Private Message
function import_pm()
{

	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "pmtext");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getfrompm = $DB->sql_query("SELECT * FROM " . $table_begin . "pmtext LIMIT $count,$perpage");

    while ($getfromrow = $DB->sql_fetch_array($getfrompm))
    {
    	$to  = $getfromrow['touserarray'];
        $to  = explode('"',$to);
        $num = 1;

        while ($num < count($to))
        {
        	$pm_date = $this->vbdate("d/m/Y", $getfromrow['dateline'], true);

            $insertnpm = $DB->sql_query("INSERT INTO MySBB_pm(id,title,text,user_from,user_to,user_read,icon,folder,date) VALUES
                                                             ('NULL','" .  $this->change_string($getfromrow['title']) . "','" .  $this->change_string($getfromrow['message']) . "','" .  $this->change_string($getfromrow['fromusername']) . "','" . $to[$num] . "','1','image/icons/i1.gif','inbox','" . $pm_date . "')");

            if ($insertnpm)
            {
                    $SF->msg("تم نقل " . $this->change_string(htmlspecialchars($getfromrow['pmtextid'])));
            }
            if (!$insertnpm)
            {
                    $SF->msg("لم يتم نقل " . $this->change_string(htmlspecialchars($getfromrow['pmtextid'])));
            }

            $num = $num+2;
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
    	echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=8'>تحويل الردود</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=8&count=$next'>$i</a>";
                }

                        $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=8&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

 # Reply
function import_reply()
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "post WHERE parentid<>'0'");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getfromreply = $DB->sql_query("SELECT * FROM " . $table_begin . "post WHERE parentid<>'0' LIMIT $count,$perpage");

    while ($getfromrow = $DB->sql_fetch_array($getfromreply))
    {

    	$date = $this->vbdate("j/n/Y", $getfromrow['dateline'], true);
        $time = $this->vbdate("h:i", $getfromrow['dateline']);

        $insertreply = $DB->sql_query("INSERT INTO MySBB_reply
                   (id,title,text,subject_id,writer,write_date,icon,gmt_time)
                   VALUES
                   ('" . $getfromrow['postid'] . "','" .  $this->change_string(addslashes($getfromrow['title'])) . "','" .  $this->change_string(addslashes($getfromrow['pagetext'])) . "','" . $getfromrow['parentid'] . "','" .  $this->change_string($getfromrow['username']) . "','" .  $date . "','i1.gif','" . $time . "')");

        if ($insertreply)
        {
                $SF->msg("تم نقل " . $getfromrow['postid']);
        }
        if (!$insertreply)
        {
                $SF->msg("لم يتم نقل " . $getfromrow['postid']);
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=9'>تحويل المواضيع</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=8&count=$next'>$i</a>";
                }

                        $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=8&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/


 # Subject

function import_subject()
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "post WHERE parentid='0'");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getfromsubject = $DB->sql_query("SELECT * FROM " . $table_begin . "post WHERE parentid='0' LIMIT $count,$perpage");

    while ($getfromrow = $DB->sql_fetch_array($getfromsubject))
    {
    	$getsubject_query = $DB->sql_query("SELECT * FROM " . $table_begin . "thread WHERE threadid='" . $getfromrow['threadid'] . "'");
        while ($getsubject_row = $DB->sql_fetch_array($getsubject_query))
        {
        	# $getsubject_row from thread table
            # $getfromrow from post table

            $date = $this->vbdate("j/n/Y", $getfromrow['dateline'], true);
            $time = $this->vbdate("h:i", $getfromrow['dateline']);


            $insertreply = $DB->sql_query("INSERT INTO MySBB_subject
            (id,title,text,writer,section,write_date,reply_number,visitor,write_time,reply_time,icon,wr_date,native_write_time,gmt_time) VALUES
            ('" . $getfromrow['postid'] . "','" .  $this->change_string($getfromrow['title']) . "','" .  $this->change_string(addslashes($getfromrow['pagetext'])) . "','" . $this->change_string($getfromrow['username']) . "','" . $getsubject_row['forumid'] . "','" .  $date . "','" . $getsubject_row['replycount'] . "','" . $getsubject_row['views'] . "','" . $getsubject_row['lastpost'] . "','" . $getsubject_row['lastpost'] . "','i1.gif','" . $date . "','" . $getsubject_row['lastpost'] . "','" . $time . "')");

            if ($insertreply)
            {
                    $SF->msg("تم نقل " . $this->change_string(htmlspecialchars($getfromrow['postid'])));
            }
            if (!$insertreply)
            {
                    $SF->msg("لم يتم نقل " . $this->change_string(htmlspecialchars($getfromrow['postid'])));
            }
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=10'>انشاء مجموعات الاقسام</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=9&count=$next'>$i</a>";
                }

                $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=9&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

 # Info
function import_info()
{

	global $DB,$SF;

    $gettitle = $DB->sql_query("SELECT * FROM " . $table_begin . "setting WHERE varname='bbtitle'");
    $titlerow = $DB->sql_fetch_array($gettitle);

    $getemail = $DB->sql_query("SELECT * FROM " . $table_begin . "setting WHERE varname='webmasteremail'");
    $emailrow = $DB->sql_fetch_array($getemail);

    $insert = $DB->sql_query("INSERT INTO MySBB_info
    (id,title,perpage,subject_perpage,icon_path,send_email,avatar_perpage,admin_email,MySBB_version,fastreply_allow,download_path,upfile_size,def_group,adef_group,def_style,page_max) VALUES
    ('1','" . $this->change_string(addslashes($titlerow['value'])) . "',15,15,'image/icons/','" . $emailrow['value'] . "',5,'" . $emailrow['value'] . "','1.1.2',1,'download',10240,5,4,1,5)");

    if ($insert)
    {
            $SF->msg("تم نقل المعلومات");
    }
}

/************************************************/

function import_usertitle()
{
	global $DB,$SF;

    $getusertitle_query = $DB->sql_query('SELECT * FROM ' . $table_begin . 'usertitle');

    while ($titlerow = $DB->sql_fetch_array($getusertitle_query))
    {
    	$insert_title = $DB->sql_query("INSERT INTO MySBB_usertitle(id,usertitle,posts) VALUES('NULL','" . $this->change_string(addslashes($titlerow['title'])) . "','" . $titlerow['minposts'] . "')");

        if ($insert_title)
        {
                $SF->msg("تم اضافة " . $titlerow['usertitleid']);
        }
        if (!$insert_title)
        {
                $SF->msg("لم يتم اضافة " . $titlerow['usertitleid']);
        }
    }
}

/************************************************/

function import_poll()
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "poll");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getpoll_query = $DB->sql_query("SELECT * FROM " . $table_begin . "poll LIMIT $count,$perpage");

    while ($row = $DB->sql_fetch_array($getpoll_query))
    {
    	$answer = explode('|||',$row['options']);
        $result = explode('|||',$row['votes']);

        $getsubjectid_query = $DB->sql_query("SELECT * FROM " . $table_begin . "thread WHERE pollid='" . $row['pollid'] . "'");
        while ($subjectrow = $DB->sql_fetch_array($getsubjectid_query))
        {
        	$insert = $DB->sql_query("INSERT INTO MySBB_poll(id,qus,ans1,ans2,ans3,ans4,ans5,ans6,ans7,ans8,res1,res2,res3,res4,res5,res6,res7,res8,subject_id) VALUES('" . $row['pollid'] . "','" . $this->change_string($row['question']) . "','" . $this->change_string($answer[0]) . "','" . $this->change_string($answer[1]) . "','" . $this->change_string($answer[2]) . "','" . $this->change_string($answer[3]) . "','" . $this->change_string($answer[4]) . "','" . $this->change_string($answer[5]) . "','" . $this->change_string($answer[6]) . "','" . $this->change_string($answer[7]) . "','" . $this->change_string($result[0]) . "','" . $this->change_string($result[1]) . "','" . $this->change_string($result[2]) . "','" . $this->change_string($result[3]) . "','" . $this->change_string($result[4]) . "','" . $this->change_string($result[5]) . "','" . $this->change_string($result[6]) . "','" . $this->change_string($result[7]) . "','" . $subjectrow['firstpostid'] . "')");

            if ($insert)
            {
                    $SF->msg("تم نقل " . $row['pollid']);
            }
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=15'>تحويل الاقتراع</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=14&count=$next'>$i</a>";
                }

                        $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=14&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

function import_vote()
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "pollvote");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getvote_query = $DB->sql_query("SELECT * FROM " . $table_begin . "pollvote LIMIT $count,$perpage");

    while ($row = $DB->sql_fetch_array($getvote_query))
    {
    	$insert = $DB->sql_query("INSERT INTO MySBB_vote(id,poll_id,member_id) VALUES('" . $row['pollvoteid'] . "','" . $row['pollid'] . "','" . $row['userid'] . "')");

        if ($insert)
        {
                $SF->msg("تم نقل" . $row['pollvoteid']);
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=16'>تعديل اعداد الردود</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=14&count=$next'>$i</a>";
                }

                        $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=14&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

function import_admin()
{
        global $SF;

        $SF->html('
          <p align="center">
          <center>
          <form method="POST" action="index.php?step=20">
          <p>يرجى كتابة اسم المستخدم الخاص بالمدير</p>
          <input type="text" name="T1" size="27" style="font-family: Tahoma; font-size: 8pt">
          <p>يرجى تحديد كلمه سريه جديده للمدير</p>
          <input type="password" name="T2" size="27" style="font-family: Tahoma; font-size: 8pt">
          <input type="submit" value="موافق" name="B1" style="font-family: Tahoma; font-size: 8pt"></p>
          </form>');
}

/************************************************/

function import_admin_start()
{
		  global $SF,$DB,$_POST;

          if (empty($_POST['T1'])) { $SF->error("يرجى كتابة الاسم"); }
          if (empty($_POST['T2'])) { $SF->error('يرجى كتابة كلمة المرور الجديده'); }

          $get_query = $DB->sql_query("SELECT * FROM MySBB_member WHERE username='" . $_POST['T1'] . "'");
          $get_row   = $DB->sql_fetch_array($get_query);
          $get_num         = $DB->sql_num_rows($get_query);

          if ($get_num <= 0)
          {
				$SF->error('المعذره .. العضو المطلوب غير موجود');
          }

          $update = $DB->sql_query("UPDATE MySBB_membergroup SET group_id='1' WHERE member_id='" . $get_row['id'] . "'");

          if ($update)
          {
          		$SF->msg("تم تعيين " . $_POST['T1'] . " كمدير");
          }
          else
  		  {
          		$SF->msg("فشل في تعيين " . $_POST['T1']);
          }

          $password = md5($_POST['T2']);

          $update_password = $DB->sql_query("UPDATE MySBB_member SET password='$password' WHERE username='" . $_POST['T1'] . "'");

          if ($update_password)
          {
          		$SF->msg('تم تعيين كلمة المرور');
          }
          else
          {
          		$SF->msg('لم يتم تعيين كلمة المرور');
          }

          $SF->msg("تم تحويل المنتدى بالكامل بفضل الله");
}

/************************************************/

function fix_reply_number()
{

    global $SF,$DB,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM MySBB_reply");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;


    $get_reply = $DB->sql_query("SELECT * FROM MySBB_reply LIMIT $count,$perpage");
    while ($get_row = $DB->sql_fetch_array($get_reply))
    {
    	$get_subject = $DB->sql_query("SELECT * FROM MySBB_subject WHERE id='" . $get_row['subject_id'] . "'");
        while ($get_subject_row = $DB->sql_fetch_array($get_subject))
        {
        	$update = $DB->sql_query("UPDATE MySBB_reply SET section='" . $get_subject_row['section'] . "' WHERE id='" . $get_row['id'] . "'");
            if ($update)
            {
                    $SF->msg('تم ' . $get_row['id']);
            }
            else
            {
                    $SF->msg('لم يتم ' . $get_row['id']);
            }
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='index.php?step=17'>تحويل المشرفين</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='index.php?step=16&count=$next'>$i</a>";
                }

                $next=$next+$perpage;
                $i++;
        }

        $SF->goto('index.php?step=16&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }
}

/************************************************/

function import_moderator()
{
	global $SF,$DB;

    $get_moderator = $DB->sql_query('SELECT * FROM ' . $table_begin . 'moderator');

    while ($moderator_row = $DB->sql_fetch_array($get_moderator))
    {
    	$insert = $DB->sql_query("INSERT INTO MySBB_sectionadmin                                                                                                                          (id,section_id,member_id)
                                                          VALUES
                                    ('" . $moderator_row['moderatorid'] . "','" . $moderator_row['forumid'] . "','" . $moderator_row['userid'] . "')");

        if ($insert)
        {
                $SF->msg("تم تحويل المدير رقم " . $moderator_row['moderatorid']);
        }
        else
        {
                $SF->msg("لم يتم تحويل " . $moderator_row['moderatorid']);
        }
    }
}

/************************************************/

function change_moderator_per()
{
	global $SF,$DB;

    $get_moderator = $DB->sql_query('SELECT * FROM MySBB_sectionadmin');

    while ($moderator_row = $DB->sql_fetch_array($get_moderator))
    {

        $update = $DB->sql_query("UPDATE MySBB_membergroup SET
                                                                                                                        group_id='3'
                                                  WHERE member_id='" . $moderator_row['member_id'] . "'");

        if ($update)
        {
                $SF->msg("تم تغيير " . $moderator_row['member_id']);
        }
        else
        {
                $SF->msg("لم يتم تغيير " . $moderator_row['member_id']);
        }

    }
}

/************************************************/

function change_member_password()
{

	global $SF,$DB,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query('SELECT * FROM MySBB_member');
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 50;

    $getinfo_query = $DB->sql_query("SELECT * FROM MySBB_info WHERE id='1'");
    $info_row      = $DB->sql_fetch_array($getinfo_query);

    $getgroup_query = $DB->sql_query("SELECT * FROM MySBB_membergroup WHERE group_id<>'1' LIMIT $count,$perpage");
    while ($getgroup_row = $DB->sql_fetch_array($getgroup_query))
    {
    	$getmember_query = $DB->sql_query("SELECT * FROM MySBB_member WHERE id='" . $getgroup_row['member_id'] . "'");

    	while ($getmember_row = $DB->sql_fetch_array($getmember_query))
    	{
    		$new_password = $this->random();

        	$update = $DB->sql_query("UPDATE MySBB_member SET password='" . md5($new_password) . "' WHERE id='" . $getmember_row['id'] . "'");

        	if ($update)
        	{
        		$send_email = $this->send_mail($getmember_row['email'],'كلمة مرورك الجديده في ' . $info_row['title'],'السلام عليكم .. كلمة مرورك الجديده هي' . $new_password,$info_row['send_email']);

            	if ($send_email)
            	{
                    $SF->msg('تم ارسال كلمة المرور الجديده إلى ' . $getmember_row['username']);
            	}
            	else
            	{
                    $SF->msg('لم ينجح في ارسال كلمة المرور إلى ' . $getmember_row['username']);
            	}
        	}
    	}
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            //do nothing
    }
    else
    {
    	echo "<div align='center'>[تعداد الصفحات ";

    	while($next<=$check_end)
    	{
            if ($count==$next)
        	{
                echo " $i";
        	}
        	else
        	{
                echo " <a href='send_password.php?count=$next'>$i</a>";
        	}

        	$next=$next+$perpage;
        	$i++;
    	}

    	$SF->goto('send_password.php?count=' . ($count+$perpage));
    	echo" ]"."</div>";
	}
}

/************************************************/

function attach_from_db_to_folder($download_path)
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "attachment");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getattach_query = $DB->sql_query("SELECT * FROM " . $table_begin . "attachment LIMIT $count,$perpage");

    while ($getattach_row = $DB->sql_fetch_array($getattach_query))
    {
    	$fp = fopen($download_path . $getattach_row['filename'],'w');

        if ($fp)
        {
        	$fw = fwrite($fp,$getattach_row['filedata']);

            if ($fw)
            {
                    $SF->msg('تم تحويل ' . $getattach_row['filename']);
            }
            else
            {
                    $SF->msg('لم يتم تحويل ' . $getattach_row['filename']);
            }
        }

        else
        {
                $SF->msg('لم يتم فتح ' . $getattach_row['filename']);
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
            echo "<div align='center'>[الخطوه التاليه <a href='$PHPSELF?step=2'>تحويل بيانات قواعد البيانات</a> ]"."</div>";
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='convert_attach.php?step=1&count=$next'>$i</a>";
                }

                $next=$next+$perpage;
                $i++;
        }

        $SF->goto('convert_attach.php?step=1&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }

}

/************************************************/

function attach_from_db_to_db($download_path)
{
	global $DB,$SF,$_GET;

    $count = $_GET['count'];

    if (empty($count)) { $count=0; }

    $TotalRecords = $DB->sql_query("SELECT * FROM " . $table_begin . "attachment");
    $check_end    = $DB->sql_num_rows($TotalRecords);

    $perpage      = 100;

    $getattach_query = $DB->sql_query("SELECT * FROM " . $table_begin . "attachment LIMIT $count,$perpage");

    while ($row = $DB->sql_fetch_array($getattach_query))
    {
    	$getpost_query = $DB->sql_query("SELECT * FROM " . $table_begin . "post WHERE postid='" . $row['postid'] . "'");

        while ($getpost_row = $DB->sql_fetch_array($getpost_query))
        {
        	if ($getpost_row['parentid'] == 0)
            {
            	$insert = $DB->sql_query("INSERT INTO MySBB_attach(id,filename,filepath,filesize,subject_id,visitor,reply) VALUES ('NULL','" .  $this->change_string($row['filename']) . "','" . $download_path . $this->change_string($row['filename']) . "','" . $row['filesize'] . "','" . $getpost_row['postid'] . "','" . $row['counter'] . "','0')");

                if ($insert)
                {
                        $SF->msg('تم تحويل المرفق ' . $getpost_row['filename'] . ' التابعه لموضوع');
                }
                else
                {
                        $SF->msg('لم يتم تحويل المرفق ' . $getpost_row['filename'] . ' التابعه لموضوع');
                }
            }
            else
            {
            	$insert = $DB->sql_query("INSERT INTO MySBB_attach(id,filename,filepath,filesize,subject_id,visitor,reply) VALUES ('NULL','" .  $this->change_string($row['filename']) . "','" . $download_path . $this->change_string($row['filename']) . "','" . $row['filesize'] . "','" . $getpost_row['postid'] . "','" . $row['counter'] . "','1')");

                if ($insert)
                {
                        $SF->msg('تم تحويل المرفق ' . $getpost_row['filename'] . ' التابعه لرد');
                }
                else
                {
                        $SF->msg('لم يتم تحويل المرفق ' . $getpost_row['filename'] . ' التابعه لرد');
                }
            }
        }
    }

    $i = 1;

    $next = 0;

    if ($check_end<($count+$perpage))
    {
		//do nothing
    }
    else
    {
        echo "<div align='center'>[تعداد الصفحات ";

        while($next<=$check_end)
        {
                if ($count==$next)
                {
                        echo " $i";
                }
                else
                {
                        echo " <a href='convert_attach.php?step=2&count=$next'>$i</a>";
                }

                        $next=$next+$perpage;
                $i++;
        }

        $SF->goto('convert_attach.php.php?step=2&count=' . ($count+$perpage));
        echo" ]"."</div>";
    }

}

/************************************************/

}

?>